/*
This file is part of asproxy.

asproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

asproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with asproxy.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "log.h"

void log_open()
{
	openlog("asproxy", LOG_PID | LOG_NDELAY, LOG_LOCAL0);	
}

void log_close()
{
	closelog();
}

void log_message(int priority, char* message)
{
#ifdef _LINUX32BIT_
	syslog(priority, "[tid:%ld]: %s", (long int)syscall(224), message);
#else
	syslog(priority, "[tid:%ld]: %s", (long int)syscall(186), message);
#endif
}

void log_error(char* message)
{
	log_message(LOG_ERR, message);
}

void log_errorcode(char* message, int errno)
{
	char errorstring[250] = {0};
	sprintf(errorstring, "%s: %d", message, errno);
	log_error(errorstring);	
}

void log_warning(char* message)
{
	log_message(LOG_WARNING, message);
}

void log_info(char* message)
{
	log_message(LOG_INFO, message);
}

void log_debug(char* message)
{
	log_message(LOG_DEBUG, message);
}

void log_transport(char* type, char* message)
{
	if (g_proxyconfig.Debug == 'Y')
	{
#ifdef _LINUX32BIT_
	syslog(LOG_DEBUG, "[tid:%ld]: %s|%s", (long int)syscall(224), type, message);
#else
	syslog(LOG_DEBUG, "[tid:%ld]: %s|%s", (long int)syscall(186), type, message);
#endif
	}
}

/*
If you want to push logs to another server rather than on local /var/logs/
edit 
#/etc/rsyslog.conf
$ModLoad imfile
$InputFileName /data/mysql/error.log
$InputFileTag mysql-error
$InputFileStateFile stat-mysql-error
$InputFileSeverity error
$InputFileFacility local3
$InputRunFileMonitor
local3.* @@hostname:<portnumber>
 * 
 * Make sure you update the tcp_max_syn_backlog to a big number otherwise, the kernel will kill your process
 * /proc/sys/net/ipv4$ sudo pico tcp_max_syn_backlog

*/